package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/22 19:16
 * @since JDK 1.8
 */
@WebServlet("/editBook")
public class EditBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        Map<String, Object> returnData = new HashMap<>();
        int result = 0;
        if ("insertBookSortInfo".equals(method)){
            result = insertBookSortInfo(req,resp);
        }
        // 操作是否成功的判断
        if (result >= 1) {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnMsg", ReturnCode.SUCCESS.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnData", ReturnCode.ERROR.getMsg());
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 新增图书分类的方法
    private int insertBookSortInfo(HttpServletRequest req, HttpServletResponse resp) {
        String tsb = req.getParameter("tsb");
        String ssb = req.getParameter("ssb");
        String tsbAddEither = req.getParameter("tsbAddEither");
        return new BookServiceImpl().insertBookSort(tsb,ssb,tsbAddEither);
    }
}
